// pages/TradingAreaCenter/addCouponBag.js
const controller = require("../controllers/Controller").controller

var dateTimePicker = require('../../utils/dateTimePicker.js');


const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:true,
    isUpdata: true,
    value:{},
    otherValue:{},
    icon1: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210513/842452706747482112.png",
    icon2: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210513/842452919725850624.png",
    payTypeList: [{
      name: "单次购买",
      id: 1
    },{
      name: "无限制",
      id: 0
    }],
    shopGroupList: [],
    shopList: [],
    type: 0,
    img: "",
    startTime: "",
    endTime: "",
    baseUrl: "",
    payType: 1,
    num: "",
    index: "",
    shopGroupId: "",
    shopId: "",
    price: "",
    detailImg: "",
    sort: "",
    couponList: [],
    cardList: [],
    shopGroupName: "",
    useRules: "",
    noSelectIcon: 'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210513/842452706747482112.png',
    selectedIcon: 'https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210513/842452919725850624.png',
    activitySelectList: [{
      name: "系统默认",
      id: 0
    }, {
      name: "自定义",
      id: 1
    }],
    activityRuleIndex: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const value = JSON.parse(options.value)
    const {repeatList,repeatCardList,shopGroupName,couponList,cardList} = JSON.parse(options.otherValue)
    console.log(value);
    this.setData({
      value,
      repeatList,
      repeatCardList,
      shopGroupName,
      couponList,
      cardList,
      baseUrl: app.globalData.IMG_URL
    })
    this.setBagDetail(value)
    
  },
  
  //打开预览图片
  openBigImg() {
    if (!this.data.detailImg) {
      return
    }
    let that = this
    let list = []
    list.push(that.data.detailImg)
    wx.previewImage({
      current: that.data.detailImg,
      urls: list
    })
  },

  //写入券礼包详情
  setBagDetail(value) {
    let list = []
        if (value.couponMap) {
          let keys = Object.keys(value.couponMap)
          keys.forEach(item => {
            for (let i = 0; i < value.couponMap[item].length; i++) {
              list.push(value.couponMap[item][i])
            }
          })
        }
        this.setData({
          type: value.type,
          img: value.img,
          startTime: value.startTime.substr(0,16),
          endTime: value.endTime.substr(0,16),
          payType: value.payType,
          num: value.num,
          shopGroupId: value.shopGroupId,
          // shopId:res.data,
          price: value.price,
          detailImg: value.detailImg,
          sort: value.sort,
          couponList: list,
          cardList: value.vipList,
          cost: value.cost,
          name: value.name,
          goodsList: value.goodsList,
          useRules: value.useRules,
          activityRuleIndex:value.useRulesType
        })
  },

  backPage(){
    wx.navigateBack()
  },


  isAddBags(){
    const that = this
    wx.showModal({
      title: '是否确认提交',
      success: function(res) {
        if (res.confirm) {
          that.addBags()
        }
      }
    })
  },

  //新增券礼包
  addBags() {
    const value = this.data.value


    controller.addBagSave(value).then(res => {
      if (res.code == 1) {
        wx.showToast({
          icon: "none",
          title: '添加成功',
          duration: 1000
        })
        setTimeout(function () {
          wx.redirectTo({
            url: '/pages/TradingAreaCenter/submitted',
          })
        }, 1000)
      }
    })

  },




 

  
})